package cn.topevery.garden.wapi.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.topevery.garden.wapi.model.GreTreeFamousExcel;
import cn.topevery.garden.wapi.model.SysDeptExcel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

@Slf4j
@Component
public class ExcelUtils {


    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams);
    }

    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams(title, sheetName);
        defaultExport(list, pojoClass, fileName, response, exportParams);
    }

    public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
        defaultExport(list, fileName, response);
    }

    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        if (workbook != null) {
            downLoadExcel(fileName, response, workbook);
        }
    }

    private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null) {
            downLoadExcel(fileName, response, workbook);
        }
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {

        }
    }


    public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception {
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(titleRows);
        importParams.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, importParams);
        } catch (NoSuchElementException e) {
            throw e;
        } catch (Exception e) {
            throw e;
        }
        return list;
    }

    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headRows, Class<T> pojoClass) throws Exception {
        if (file == null) {
            return null;
        }
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(titleRows);
        importParams.setHeadRows(headRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, importParams);
        } catch (NoSuchElementException e) {
            throw e;
        } catch (Exception e) {
            throw e;
        }
        return list;
    }

    public  List<SysDeptExcel> readShp(String shpPath){
        File f;
        f = new File(shpPath);
        File[] files = f.listFiles(); // 得到f文件夹下面的所有文件。
        for (File file : files) {
            if(file.isDirectory()) {
                //如何当前路径是文件夹，则循环读取这个文件夹下的所有文件
                readShp(file.getAbsolutePath());
            } else {
                return AnalysisExcelData(file);
            }
        }

        return null;

    }

    public List<SysDeptExcel> AnalysisExcelData(@RequestParam("file")File file) {
        try {
            List<SysDeptExcel> list = importExcel(file, 0, 1, SysDeptExcel.class);
//            System.out.printf(list.toString()+'\n');
            return list;
        } catch (Exception e) {
            System.out.printf(e.toString());
            return null;
        }

    }

    public static <T> List<T> importExcel(File file, Integer titleRows, Integer headRows, Class<T> pojoClass) throws Exception {
        if (file==null){
            return null;
        }
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(titleRows);
        importParams.setHeadRows(headRows);
        List<T> list= null;
        try {
            list = ExcelImportUtil.importExcel(file, pojoClass, importParams);
        } catch (NoSuchElementException e) {
            throw e;
        } catch (Exception e) {
            throw e;
        }
        return list;
    }
}
